import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:geomancy/mock/health_data.dart';
import 'package:geomancy/styles/index.dart';
import 'package:geomancy/tools/constant.dart';
import 'package:geomancy/tools/utils.dart';
import 'package:geomancy/widgets/common_header.dart';
import 'package:geomancy/widgets/common_text.dart';
import 'package:geomancy/widgets/common_title.dart';
import 'package:geomancy/widgets/common_vertical_text.dart';
import 'package:go_router/go_router.dart';
import 'dart:async';

import 'package:lunar/lunar.dart';

class HealthTimeMaintenance extends StatefulWidget {
  const HealthTimeMaintenance({super.key});

  @override
  State<HealthTimeMaintenance> createState() => _HealthTimeMaintenanceState();
}

class _HealthTimeMaintenanceState extends State<HealthTimeMaintenance> {
  // 定时器
  late Timer _timer;
  // 时分秒的状态变量
  late String _hours = '';   // 小时
  late String _minutes = ''; // 分钟
  late String _seconds = ''; // 秒
  late String ganZhiTime;//天干与地支时辰
  late int timeIndex;

  @override
  void initState() {
    super.initState();
    _updateTime(); // 初始化时间
    // 创建定时器，每秒更新一次时间
    _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
      _updateTime();
    });
  }

  @override
  void dispose() {
    _timer.cancel(); // 销毁定时器，避免内存泄漏
    super.dispose();
  }

  // 更新时间的方法
  void _updateTime() {
    final now = DateTime.now(); // 获取当前时间

    final ganZhiResult = Utils.getLunarTime(now);
    // 获取时辰的地支
    String hm = '${now.hour.toString().padLeft(2, '0')}:${now.minute.toString().padLeft(2, '0')}';
    int timeZhiIndex = LunarUtil.getTimeZhiIndex(hm) + 1; // 时辰地支序号

    setState(() {
      _hours = now.hour.toString().padLeft(2, '0');     // 更新小时
      _minutes = now.minute.toString().padLeft(2, '0');  // 更新分钟
      _seconds = now.second.toString().padLeft(2, '0');  // 更新秒
      ganZhiTime = '${ganZhiResult['ganZhiYear']}年 ${ganZhiResult['ganZhiMonth']}月 ${ganZhiResult['ganZhiDay']}日 ${ganZhiResult['ganZhiTime']}时';
      timeIndex = timeZhiIndex;
    });
  }

  backHandle() {
    context.go('/');
  }

  Widget timer() {
    return Row(
      children: [
        // 小时显示框
        Container(
          width: 40.w,
          height: 40.w,
          decoration: BoxDecoration(
            color: ColorStyle.themeColor,
            borderRadius: BorderRadius.all(Radius.circular(8.w))
          ),
          alignment: Alignment.center,
          child: CommonText(txt: _hours, fontSize: 24.sp),
        ),
        CommonText(txt: ':', fontSize: 24.sp),
        // 分钟显示框
        Container(
          width: 40.w,
          height: 40.w,
          decoration: BoxDecoration(
              color: ColorStyle.themeColor,
              borderRadius: BorderRadius.all(Radius.circular(8.w))
          ),
          alignment: Alignment.center,
          child: CommonText(txt: _minutes, fontSize: 24.sp),
        ),
        CommonText(txt: ':', fontSize: 24.sp),
        // 秒钟显示框
        Container(
          width: 40.w,
          height: 40.w,
          decoration: BoxDecoration(
              color: ColorStyle.themeColor,
              borderRadius: BorderRadius.all(Radius.circular(8.w))
          ),
          alignment: Alignment.center,
          child: CommonText(txt: _seconds, fontSize: 24.sp),
        )
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: CommonHeader.appBar(context: context),
      backgroundColor: ColorStyle.pageDefaultColor,
      body: ListView(
        physics: const ClampingScrollPhysics(),
        padding: EdgeInsets.fromLTRB(Constant.PAGEPADDING.w, 20.w, Constant.PAGEPADDING.w, 80.w),
        children: [
          const CommonTitleWidget(txt: '十二时辰养生'),
          Container(
            margin: EdgeInsets.only(top: 20.w, bottom: 20.w),
            padding: EdgeInsets.only(left: 40.w, right: 40.w, bottom: 40.w),
            decoration: BoxDecoration(
              color: ColorStyle.themeLightColor,
              border: Border.all(width: 5.w, color: ColorStyle.borderColor),
              borderRadius: BorderRadius.all(Radius.circular(60.w))
            ),
            child: Column(
              children: [
                ///顶部-时辰
                Container(
                  width: 510.w,
                  height: 100.w,
                  padding: EdgeInsets.symmetric(horizontal: 18.w),
                  decoration: BoxDecoration(
                    color: ColorStyle.themeLightColor,
                    border: Border(
                      left: BorderSide(width: 5.w, color: ColorStyle.borderColor),
                      bottom: BorderSide(width: 5.w, color: ColorStyle.borderColor),
                      right: BorderSide(width: 5.w, color: ColorStyle.borderColor),
                    ),
                    borderRadius: BorderRadius.only(
                      bottomLeft: Radius.circular(40.w),
                      bottomRight: Radius.circular(40.w)
                    )
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      CommonText(txt: ganZhiTime, fontSize: 24.sp),
                      timer()
                    ],
                  ),
                ),
                ///中间-图片
                SizedBox(
                  width: 624.w,
                  height: 624.w,
                  child: Image.asset('assets/grid/health_compass_time_$timeIndex.png', width: 624.w),
                ),
                Container(
                  width: 350.w,
                  height: 96.w,
                  padding: EdgeInsets.all(5.w),
                  decoration: BoxDecoration(
                      color: ColorStyle.themeColor,
                      borderRadius: BorderRadius.all(Radius.circular(20.w))
                  ),
                  child: Container(
                    alignment: Alignment.center,
                    decoration: BoxDecoration(
                      color: const Color.fromRGBO(219, 207, 174, 1),
                      border: Border.all(width: 3.w, color: ColorStyle.borderColor),
                      borderRadius: BorderRadius.all(Radius.circular(20.w))
                    ),
                    child: CommonText(txt: HealthData.timeData[timeIndex.toString()]['title'], weight: FontWeight.bold)
                  ),
                ),
                SizedBox(height: 40.w,),
                CommonText(txt: HealthData.timeData[timeIndex.toString()]['content'], fontSize: 26.sp)
              ],
            ),
          ),
          Padding(
            padding: EdgeInsets.symmetric(horizontal: 10.w),
            child: CommonText(
              txt: '根据中医理论和经络养生原则提供参考，但每个人的具体情况可能会有差异，请根据自身情况进行调整和适应。如有健康问题或身体不适，请咨询专业医生的建议。',
              fontSize: 20.sp,
              textAlign: TextAlign.center,
            ),
          )
        ],
      ),
    );
  }
}
